package ext.acae2.document.dataUtility;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wt.access.AccessControlHelper;
import wt.access.AccessPermission;
import wt.doc.WTDocument;
import wt.fc.ObjectIdentifier;
import wt.fc.PersistenceHelper;
import wt.fc.WTObject;
import wt.httpgw.URLFactory;
import wt.util.WTException;
import wt.vc.wip.WorkInProgressHelper;

import com.ptc.core.components.descriptor.ComponentDescriptor;
import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.AbstractDataUtility;
import com.ptc.core.components.factory.dataUtilities.AttributeDataUtilityHelper;
import com.ptc.core.components.rendering.GuiComponent;
import com.ptc.core.components.rendering.PickerRenderConfigs;
import com.ptc.core.components.rendering.guicomponents.GUIComponentArray;
import com.ptc.core.components.rendering.guicomponents.IconComponent;
import com.ptc.core.components.rendering.guicomponents.PickerInputComponent;
import com.ptc.core.components.rendering.guicomponents.StringInputComponent;
import com.ptc.core.components.rendering.guicomponents.TextBox;
import com.ptc.core.components.rendering.guicomponents.TextDisplayComponent;
import com.ptc.core.components.rendering.guicomponents.UrlDisplayComponent;
import com.ptc.core.components.util.OidHelper;
import com.ptc.core.meta.common.TypeIdentifierHelper;
import com.ptc.core.meta.common.impl.WCTypeInstanceIdentifier;
import com.ptc.core.meta.type.common.impl.DefaultTypeInstance;
import com.ptc.core.ui.resources.ComponentMode;

import ext.acae2.util.IBAHelper;
import ext.acae2.utilities.docpart.CSCDoc;

/**
 * @desription ECM文件应用的定制化
 * @author cnie
 * @date 2013-7-11 下午9:22:01
 */
public class ECMRecommendDataUtility extends AbstractDataUtility {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ptc.core.components.descriptor.DataUtility#getDataValue(java.lang
	 * .String, java.lang.Object,
	 * com.ptc.core.components.descriptor.ModelContext)
	 */
	@Override
	public Object getDataValue(String column, Object obj,
			ModelContext modelContext) throws WTException {
		GUIComponentArray guicomponentarrayMain = new GUIComponentArray();
		if ("replyDocID".equals(column)) {
			
			URLFactory uf = new URLFactory();
			ComponentMode compModel = modelContext.getDescriptorMode();
			if (ComponentMode.CREATE.equals(compModel)
					|| ComponentMode.EDIT.equals(compModel)) {
				String createType ="";
				if(ComponentMode.CREATE.equals(compModel)){
					createType = (String) ((List)modelContext.getNmCommandBean().getComboBox().get("createType")).get(0); //createType
				}
				String columnName = AttributeDataUtilityHelper.getColumnName(
						column, obj, modelContext);

				TextBox nInput = new TextBox();
				nInput.setInputType("text");
				nInput.setWidth(50);
				nInput.setReadOnly(true);
				nInput.setColumnName(columnName);
				nInput.setId("ECMRecommendName");
				nInput.setLabel(getLabel(column, modelContext));
				// nInput.setName("ECMRecommendName");

				if (ComponentMode.EDIT.equals(compModel)) {
					WTDocument editDoc = null;
					if (obj instanceof DefaultTypeInstance) {

						DefaultTypeInstance defaultTypeInstance = (DefaultTypeInstance) obj;
						// String iden =
						// defaultTypeInstance.getPersistenceIdentifier();
						editDoc = (WTDocument) OidHelper.getObjectReference(
								defaultTypeInstance).getObject();
					} else if (obj instanceof WTDocument) {
						editDoc = (WTDocument) obj;
					}

					if (!WorkInProgressHelper.isWorkingCopy(editDoc)) {
						editDoc = (WTDocument) WorkInProgressHelper.service
								.workingCopyOf(editDoc);
					}
					
					createType = TypeIdentifierHelper.getType(editDoc).toExternalForm();

					String recommendDocValue = IBAHelper.getIBAStringValue(
							editDoc, column);
					nInput.setValue(recommendDocValue);
				}

				TextDisplayComponent tdc = new TextDisplayComponent("");

				String imgInfo = uf.getHREF("netmarkets/images/search.gif",
						true);
				
				HashMap<String, String> params = new HashMap<String, String>();
				
				if(createType.indexOf("com.acae.externalECM")>-1){
					params.put("searchECMType", "WCTYPE|wt.doc.WTDocument|com.ptc.ReferenceDocument|com.acae.ECM");
				}else if(createType.indexOf("com.acae.ECM")>-1){
					params.put("searchECMType", "WCTYPE|wt.doc.WTDocument|com.ptc.ReferenceDocument|com.acae.ECM|com.acae.externalECM");
				}
				
				String url = uf.getHREF(
						"netmarkets/jsp/ext/acae2/document/ecm/addDoc.jsp", params,
						true);

				StringBuffer linkInfoSb = new StringBuffer();

				linkInfoSb.append("<a href=\" javascript:void(0);\"  onclick=");
				linkInfoSb
						.append("\"window.open('"
								+ url
								+ "','','toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');return false\">");
				linkInfoSb.append("<img src='" + imgInfo + "' border=0 /></a>");
				tdc.setCheckXSS(false);
				tdc.setValue(linkInfoSb.toString());
				guicomponentarrayMain.addGUIComponent(nInput);
				guicomponentarrayMain.addGUIComponent(tdc);

			} else if (ComponentMode.VIEW.equals(compModel)) {
				TextDisplayComponent tdc = new TextDisplayComponent("");
				String recommendDocValue = IBAHelper.getIBAStringValue(
						(WTObject) obj, column);
				if(recommendDocValue==null||"".equals(recommendDocValue)){
					tdc.setValue(recommendDocValue);
				}else{
					String recommandDocNumber =recommendDocValue.trim();
					if(recommendDocValue.indexOf("(")>-1){
						recommandDocNumber = recommendDocValue.substring(0,
								recommendDocValue.indexOf("("));
					}
					WTDocument recommendDoc = CSCDoc.getDoc(recommandDocNumber);
					if(recommendDoc==null){
						tdc.setValue(recommendDocValue);
					}else{
						boolean hasRead = AccessControlHelper.manager.hasAccess(
								recommendDoc, AccessPermission.READ);
						
						if (hasRead) {
							
							ObjectIdentifier oi = PersistenceHelper
									.getObjectIdentifier(recommendDoc);
							String recommendDocOid = oi.getStringValue();
							
							HashMap<String, String> params = new HashMap<String, String>();
							params.put("oid", "OR:" + recommendDocOid);
							String infoUrl = uf.getHREF("app/#ptc1/tcomp/infoPage",
									params, true);
							StringBuffer linkInfoSb = new StringBuffer();
							linkInfoSb
									.append("<a href=\" javascript:void(0);\"  onclick=");
							linkInfoSb
									.append("\"window.open('"
											+ infoUrl
											+ "','');return false\">");
							linkInfoSb.append(recommendDocValue + "</a>");
							tdc.setCheckXSS(false);
							tdc.setValue(linkInfoSb.toString());
						} else {
							tdc.setValue(recommendDocValue);
						}
					} 
				}
				
				guicomponentarrayMain.addGUIComponent(tdc);
			}
		}

		return guicomponentarrayMain;

	}

}
